<?php

namespace App\Models;

use App\Models\Conf\Agent;
use App\Models\Conf\Site;
use Auth;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Storage;
use Spatie\Permission\Traits\HasRoles;
use App\Models\Traits\WithOrderHelper;
use App\Events\BehaviorLogEvent;
use Illuminate\Database\Eloquent\SoftDeletes;


class User extends Authenticatable implements JWTSubject
{
    use SoftDeletes;

    const USER_TYPE_SYS = 0;
    const USER_TYPE_AGENT = 1;
    const USER_TYPE_SITE = 2;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'name', 'phone', 'email', 'password', 'avatar', 'introduction', 'status', 'type', 'weixin_openid', 'weixin_unionid', 'weibo_id', 'qq_id', 'github_id', 'last_ip', 'last_location', 'last_time', 'backend_ips', 'api_ips'
    ];

//    public $dispatchesEvents  = [
//        'saved' => BehaviorLogEvent::class,
//    ];
//
    public function titleName()
    {
        return 'name';
    }

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    use HasRoles;
    use WithOrderHelper;
    use Notifiable {
        notify as protected laravelNotify;
    }

    //json格式得字段处理转化成数组
    public function getBackendIpsAttribute($value)
    {
        return json_decode($value, true);
    }

    public function setBackendIpsAttribute($value)
    {
        $this->attributes['backend_ips'] = json_encode($value);
    }

    public function getApiIpsAttribute($value)
    {
        return json_decode($value, true);
    }

    public function setApiIpsAttribute($value)
    {
        $this->attributes['api_ips'] = json_encode($value);
    }

    public function notify($instance)
    {
        // 如果要通知的人是当前用户，就不必通知了！
        if ($this->id == Auth::id()) {
            return;
        }
        $this->increment('notification_count');
        $this->laravelNotify($instance);
    }

    public function isAuthorOf($model)
    {
        return $this->id == $model->user_id;
    }

    public function replies()
    {
        return $this->hasMany(Reply::class);
    }

    public function markAsRead()
    {
        $this->notification_count = 0;
        $this->save();
        $this->unreadNotifications->markAsRead();
    }


    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public function setPasswordAttribute($value)
    {
        // 如果值的长度等于 60，即认为是已经做过加密的情况
        if (strlen($value) != 60) {

            // 不等于 60，做密码加密处理
            $value = bcrypt($value);
        }

        $this->attributes['password'] = $value;
    }

    public function setAvatarAttribute($path)
    {
        // 如果不是 `http` 子串开头，那就是从后台上传的，需要补全 URL
//        if ( ! starts_with($path, 'http')) {
//
//            // 拼接完整的 URL
//            $path = config('app.url') . "/uploads/images/avatars/$path";
//        }

        $this->attributes['avatar'] = $path;
    }

    public function getAvatar()
    {

        if (!starts_with($this->avatar, 'http')) {
            // 拼接完整的 URL
            $this->avatar = $this->avatar ? Storage::url($this->avatar) : config('app.url') . '/images/avatar.jpg';
        }

        return $this->avatar;
    }

    public function agent()
    {
        return $this->hasOne(Agent::class, 'user_id');
    }

    public function site()
    {
        return $this->hasOne(Site::class, 'user_id');
    }

    /**
     * gameapi
     * 2018/10/9 21:22
     * Administrator
     * getConstants
     * 获取模型常量
     * @return array
     */
    public function getConstants()
    {
        $reflect = new \ReflectionClass(get_class($this));
        return $reflect->getConstants();
    }


}
